<?php defined('BASEPATH') OR exit('No direct script access allowed');

class User
{
    private $_ci;
    var $user_data;
    var $last_login;

    public function __construct()
    {
        $this->_ci = & get_instance();
        $user_id = $this->_ci->session->userdata('UID');
        $password = $this->_ci->session->userdata('PASS');
        $this->last_login = $this->_ci->session->userdata('LAST_LOGIN');
        $this->_ci->load->library('encrypt');
        if ($user_id)
            $user_id = $this->decrypt_id($user_id);
        if ($user_id && $password)
            $this->logIn($user_id, $this->_ci->encrypt->decode($password));
    }

    private function logIn($id, $password)
    {
        $this->_ci->load->model('user_model');
        $is_cacheOn = $this->_ci->db->cache_on;
        if ($is_cacheOn)
            $this->_ci->db->cache_off();
        $user = $this->_ci->user_model->validateLoginBYID($id, $password);
        if ($user)
            $this->setUser($user);
        else {
            $this->logOut();
        }
        if ($is_cacheOn)
            $this->_ci->db->cache_on();
    }

    private function encrypt_id($id)
    {
        $hashId = new hashids($this->_ci->config->item('hashIDSession_key'), 7);
        return $hashId->encrypt($id);
    }

    private function decrypt_id($id)
    {
        $hashId = new hashids($this->_ci->config->item('hashIDSession_key'), 7);
        $decrypt = $hashId->decrypt($id);
        return (!empty($decrypt) ? $decrypt[0] : false);
    }

    public function setUser($user, $long_time = false)
    {
        $this->user_data = $user;
        if ($long_time) {
            // Remember Me
        }
        $this->_ci->session->set_userdata('UID', $this->encrypt_id($user->user_seq_id));
        $this->_ci->session->set_userdata('PASS', $this->_ci->encrypt->encode($user->user_password));
    }

    public function get_value($key)
    {
        if ($this->is_session_saved()) {
            return (isset($this->user_data->$key) ? $this->user_data->$key : false);
        }
        return false;
    }

    private function is_session_saved()
    {
        return (!empty($this->user_data) ? true : false);
    }

    public function is_enabled($set_session = false)
    {
        if ($this->get_value('user_status_seq_id') == 1)
            return true;
        elseif ($this->get_value('user_status_seq_id') == 2) {
            if ($set_session)
                $this->_ci->session->set_flashdata('ERROR', $this->get_value('user_email') . ' account has Disabled');
        }
        return false;
    }

    public function is_banned($set_session = false)
    {
        if ($this->get_value('user_status_seq_id') == 3) {
            if ($set_session)
                $this->_ci->session->set_flashdata('ERROR', $this->get_value('user_email') . ' account has Banned');
            return true;
        }
        return false;
    }

    public function is_loggedIn($set_session = false)
    {
        return ($this->is_enabled($set_session) && !$this->is_banned($set_session) ? true : false);
    }

    public function is_admin($set_session = false)
    {
        if ($this->is_loggedIn($set_session)) {
            if ($this->get_value('user_type_seq_id') == 2)
                return true;
            if ($set_session)
                $this->_ci->session->set_flashdata('ERROR', $this->get_value('user_email') . ' account don\'t have Administrator privileges');
        }
        return false;
    }

    public function logOut()
    {
        $this->_ci->session->unset_userdata('UID');
        $this->_ci->session->unset_userdata('PASS');
        $this->_ci->session->unset_userdata('LAST_LOGIN');
        $this->_ci->session->unset_userdata('fb_token');
    }

    public function get_name()
    {
        return ($this->get_value('user_name') ? $this->get_value('user_name') : substr($this->get_value('user_email'), 0, 10));
    }

    public function get_id()
    {
        return $this->get_value('user_seq_id');
    }

    public function sendEmail($forUserID)
    {

    }

    public function get_Avatar($width = 0, $height = 0, $image_path = false, $email = false, $replace = true)
    {
        $return = false;
        if (!$image_path && $replace)
            $image_path = $this->get_value('user_profile_pic');
        if (!$email && $replace)
            $email = $this->get_value('user_email');
        if (!$width || !$height)
            $width = $height = 80;
        if ($image_path)
            $return = $this->_ci->image_workshop->crop_image($image_path, $width, $height, 'mm', 'avatar', true);
        if (!$return && $email)
            $return = get_gravatar($email, $width);
        return $return;
    }
}
